package com.hilaver.dzmis.web.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.product.ProductOrderItemColor;
import com.hilaver.dzmis.service.impl.OrderServiceImpl;
import com.hilaver.dzmis.util.StringUtils;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class OrderServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	private ProductOrderItem piOrder;

	private OrderServiceImpl orderService;

	public OrderServlet() {
		super();
		this.orderService = new OrderServiceImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.orderService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, orderService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			this.orderService.edit(this.piOrder);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.orderService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.orderService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setFilterParam(request);
			this.setPaginationParam(request);
			rtn = this.orderService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.orderService.getAll(this.id);
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		this.piOrder = new ProductOrderItem();
		super.setId(request);
		this.piOrder.setId(this.id);
		this.piOrder.setColorNumber(request.getParameter("colorNumber"));
		this.piOrder.setPantoneNumber(request.getParameter("pantoneNumber"));
		this.piOrder.setRealColor(request.getParameter("realColor"));
		this.piOrder.setFactoryColor(request.getParameter("factoryColor"));
		this.piOrder.setCustomerColor(request.getParameter("customerColor"));

		if (!StringUtils.isEmpty(request.getParameter("amountS"))) {
			this.piOrder.setAmountS(Integer.parseInt(request
					.getParameter("amountS")));
		} else {
			this.piOrder.setAmountS(0);
		}

		if (!StringUtils.isEmpty(request.getParameter("amountM"))) {
			this.piOrder.setAmountM(Integer.parseInt(request
					.getParameter("amountM")));
		} else {
			this.piOrder.setAmountM(0);
		}

		if (!StringUtils.isEmpty(request.getParameter("amountL"))) {
			this.piOrder.setAmountL(Integer.parseInt(request
					.getParameter("amountL")));
		} else {
			this.piOrder.setAmountL(0);
		}

		if (!StringUtils.isEmpty(request.getParameter("amountXl"))) {
			this.piOrder.setAmountXl(Integer.parseInt(request
					.getParameter("amountXl")));
		} else {
			this.piOrder.setAmountXl(0);
		}

		if (!StringUtils.isEmpty(request.getParameter("amountXxl"))) {
			this.piOrder.setAmountXxl(Integer.parseInt(request
					.getParameter("amountXxl")));
		} else {
			this.piOrder.setAmountXxl(0);
		}

		if (!StringUtils.isEmpty(request.getParameter("amountXxxl"))) {
			this.piOrder.setAmountXxxl(Integer.parseInt(request
					.getParameter("amountXxxl")));
		} else {
			this.piOrder.setAmountXxxl(0);
		}

		this.piOrder.setTotal(this.piOrder.getAmountS()
				+ this.piOrder.getAmountM() + this.piOrder.getAmountL()
				+ this.piOrder.getAmountXl() + this.piOrder.getAmountXxl()
				+ this.piOrder.getAmountXxxl());

		Integer piId = Integer.parseInt(request.getParameter("piId"));
		ProductIdentification pi = new ProductIdentification();
		pi.setId(piId);
		this.piOrder.setProductIdentification(pi);

		ProductOrderItemColor poiColorA = this.fillProductOrderItemColor("A",
				request);
		if (poiColorA != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorA);
			poiColorA.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorB = this.fillProductOrderItemColor("B",
				request);
		if (poiColorB != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorB);
			poiColorB.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorC = this.fillProductOrderItemColor("C",
				request);
		if (poiColorC != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorC);
			poiColorC.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorD = this.fillProductOrderItemColor("D",
				request);
		if (poiColorD != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorD);
			poiColorD.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorE = this.fillProductOrderItemColor("E",
				request);
		if (poiColorE != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorE);
			poiColorE.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorF = this.fillProductOrderItemColor("F",
				request);
		if (poiColorF != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorF);
			poiColorF.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorG = this.fillProductOrderItemColor("G",
				request);
		if (poiColorG != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorG);
			poiColorG.setProductOrderItem(this.piOrder);
		}

		ProductOrderItemColor poiColorH = this.fillProductOrderItemColor("H",
				request);
		if (poiColorH != null) {
			this.piOrder.getProductOrderItemColors().add(poiColorH);
			poiColorH.setProductOrderItem(this.piOrder);
		}

	}

	private ProductOrderItemColor fillProductOrderItemColor(String charactor,
			HttpServletRequest request) {
		ProductOrderItemColor poiColor = null;
		if (!StringUtils.isEmpty(request.getParameter("color" + charactor))) {
			poiColor = new ProductOrderItemColor();
			poiColor.setColorNumber(request.getParameter("color" + charactor));
			poiColor.setPantoneNumber(request.getParameter("pantoneNumber"
					+ charactor));
			poiColor
					.setRealColor(request.getParameter("realColor" + charactor));
			if (!StringUtils.isEmpty(request.getParameter("percentage"
					+ charactor))) {
				poiColor.setPercentage(Float.parseFloat(request
						.getParameter("percentage" + charactor)));
			}else{
				poiColor.setPercentage(0f);
			}
		}
		return poiColor;
	}
}